package com.anti.modular.sys.notice.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.anti.jdbc.DBUtils;
import com.anti.modular.sys.notice.entity.SysNoticeUser;
import com.anti.modular.sys.notice.service.SysNoticeUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description TODO
 * @Author anTi
 * @Date 2021-3-25
 */
@Service
public class SysNoticeUserServiceImpl implements SysNoticeUserService {
    @Resource
    DBUtils dbUtils;
    @Override
    public void add(Long noticeId, List<Long> noticeUserIdList, Integer noticeUserStatus) {
        noticeUserIdList.forEach(userId -> {
            SysNoticeUser sysNoticeUser = new SysNoticeUser();
            sysNoticeUser.setNoticeId(noticeId);
            sysNoticeUser.setUserId(userId);
            sysNoticeUser.setStatus(noticeUserStatus);
            dbUtils.insert(sysNoticeUser);
        });
    }

    @Override
    public List<SysNoticeUser> getSysNoticeUserListByNoticeId(Long noticeId) {
        return dbUtils.query("select * from sys_notice_user where notice_id=?",SysNoticeUser.class,noticeId);
    }

    @Override
    public void saveRead(Long noticeId, Long userId, Integer status) {
        SysNoticeUser sysNoticeUser = dbUtils.getFirst("select * from sys_notice_user where notice_id=? and user_id=?",SysNoticeUser.class,noticeId,userId);
        if (ObjectUtil.isNotNull(sysNoticeUser)) {
            sysNoticeUser.setStatus(status);
            sysNoticeUser.setReadTime(new Date());
            dbUtils.update(sysNoticeUser);
        }
    }

    @Override
    public void edit(Long noticeId, List<Long> noticeUserIdList, Integer noticeUserStatus) {
        //先删除
        String sql="delete from sys_notice_user where notice_id=?";
        dbUtils.execSql(sql,noticeId);
        //再增加
        this.add(noticeId, noticeUserIdList, noticeUserStatus);
    }
}
